/*
 * @Author: lei
 * @Description: 使用指针来访问private成员，突出指针的灵活强大！
 * @FilePath: /code/test_ptr_private.cc
 */
#include <iostream>
using namespace std;

class A {
 public:
  A(int a, int b, int c);
  void Show() {
    cout << "-----------" << endl;
    cout << "m_a=" << m_a << endl;
    cout << "m_b=" << m_b << endl;
    cout << "m_c=" << m_c << endl;
    cout << "-----------" << endl;
  }

 private:
  int m_a;
  int m_b;
  int m_c;
};
A::A(int a, int b, int c) : m_a(a), m_b(b), m_c(c) {}

void test() {
  A obj(10, 20, 30);
  int a1 = *(int *)&obj;
  int b = *(int *)((int *)&obj + sizeof(int));  // 地址加减

  A *p = new A(40, 50, 60);
  int a2 = *(int *)p;
  int c = *(int *)((int *)p + sizeof(int) * 2);
  p->Show();
  int *p_int = reinterpret_cast<int *>(p);
  cout << "*p=" << *p_int << endl;
  cout << "*(p+1)=" << *(p_int + 1) << endl;  // 指针加减
  cout << "*(p+2)=" << *(p_int + 2) << endl;
  *p_int = 100;  // 使用指针突破了private的限制
  p->Show();
}

int main() {
  test();
  return 0;
}